﻿#include "robotwoker.h"
#include <QDebug>
#include <QJsonParseError>
#include <QJsonObject>
#include <QCoreApplication>
#include <QThread>
#include <QJsonArray>

RobotWoker::RobotWoker(QObject *parent) : QObject(parent)
{
    m_pMySessionCB = new MySessionCB();
    connect(m_pMySessionCB,SIGNAL(sig_Notify(int,QString)),this,SLOT(slot_notify(int,QString)));

    Ice::InitializationData id;
    QString ip = "127.0.0.1";
    try{
        id.properties = Ice::createProperties();
        //所有的QString转换为std::string时需要先转换为utf-8编码
        id.properties = Ice::createProperties();
        id.properties->setProperty(QString("Ice.Default.Router").toUtf8().toStdString(), QString("Glacier2/router:tcp -p 8989 -h %1 -t 2000\r\n").arg(ip).toUtf8().toStdString());
        id.properties->setProperty(QString("Callback.CallbackServer").toUtf8().toStdString(), QString("callback:tcp -p 12345:udp -p 12345\r\n").toUtf8().toStdString());
        id.properties->setProperty(QString("Callback.Client.Endpoints").toUtf8().toStdString(), QString("tcp:udp\r\n").toUtf8().toStdString());
    }catch(Ice::Exception e){
        qDebug()<<"Init Error:"<<e.what();
    }
    try{
        _factory = new Glacier2::SessionFactoryHelper(id, m_pMySessionCB);
        //服务端对用户名和密码暂时未做管理，允许任意用户和密码登录，以后会做管理
        _session = _factory->connect("aa", "bb");//执行此语句后，MySessionCB::slot_Notify()会被调用
    }catch(Ice::Exception e){
        qDebug()<<"connect Error:"<<e.what();
    }
//    for(int i = 0;i < 100;i++){
//        QCoreApplication::processEvents();
//        QThread::msleep(5);
//    }

}

std::string RobotWoker::ToUtf8(QString string)
{
    return string.toUtf8().toStdString();
}

void RobotWoker::getPrx()
{
    try
    {
        m_prxMotor = Robot::MotorPrx::checkedCast(_session->communicator()->stringToProxy("Motor"));
        m_prxAudio = Robot::AudioPrx::checkedCast(_session->communicator()->stringToProxy("Audio"));
        m_prxSession = Robot::SessionPrx::checkedCast(_session->communicator()->stringToProxy("Session"));
        m_prxLive = Robot::LivePrx::checkedCast(_session->communicator()->stringToProxy("Live"));
        m_prxScript = Robot::ScriptPrx::checkedCast(_session->communicator()->stringToProxy("Script"));
        m_prxJson = Robot::JsonPrx::checkedCast(_session->communicator()->stringToProxy("Json"));
        m_prxConfig = Robot::ConfigPrx::checkedCast(_session->communicator()->stringToProxy("Config"));

    }catch(Ice::Exception& e)
    {
        qDebug()<<"prx Error:"<<e.what();
        return;
    }
}

void RobotWoker::slot_tts_play(QString strTTS)
{
    try{
        m_prxAudio->TtsPlay(ToUtf8(strTTS));
        qDebug()<<"TtsPlay:"<<strTTS;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"TtsPlay Error:"<<e.what();
    }
}

void RobotWoker::slot_tts_params(QString strTTS, QString strSpeaker, int nSpeed, int nPitch)
{
    try{
        m_prxAudio->TtsPlayParams(ToUtf8(strTTS),ToUtf8(strSpeaker),nSpeed,nPitch);
        qDebug()<<"TtsPlayParams:"<<strTTS;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"TtsPlayParams Error:"<<e.what();
    }
}

void RobotWoker::slot_tts_play_file(QString strFilePath)
{
    try{
        m_prxAudio->TtsPlayFile(ToUtf8(strFilePath));
        qDebug()<<"TtsPlayFile:"<<strFilePath;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"TtsPlayFile Error:"<<e.what();
    }
}

void RobotWoker::slot_tts_volume(int nVolume)
{
    try{
        m_prxAudio->TtsSetVolume(nVolume);
        qDebug()<<"TtsSetVolume:"<<nVolume;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"TtsSetVolume Error:"<<e.what();
    }
}

void RobotWoker::slot_tts_stop()
{
    try{
        m_prxAudio->TtsStop();
        qDebug()<<"TtsStop";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"TtsStop Error:"<<e.what();
    }
}

void RobotWoker::slot_tts_Playing()
{
    try{
        bool bPlaying = m_prxAudio->TtsIsPlaying();
        qDebug()<<"TtsIsPlaying:"<<bPlaying;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"TtsIsPlaying Error:"<<e.what();
    }

}

void RobotWoker::slot_media_play(QString strFilePath)
{
    try{
        m_prxAudio->MediaPlay(ToUtf8(strFilePath));
        qDebug()<<"MediaPlay:"<<strFilePath;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"MediaPlay Error:"<<e.what();
    }
}

void RobotWoker::slot_media_volume(int nVolume)
{
    try{
        m_prxAudio->MediaSetVolume(nVolume);
        qDebug()<<"MediaSetVolume:"<<nVolume;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"MediaSetVolume Error:"<<e.what();
    }
}

void RobotWoker::slot_media_stop()
{
    try{
        m_prxAudio->MediaStop();
        qDebug()<<"MediaStop";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"MediaStop Error:"<<e.what();
    }
}

void RobotWoker::slot_media_Playing()
{
    try{
        bool bPlaying = m_prxAudio->MediaIsPlaying();
        qDebug()<<"MediaIsPlaying:"<<bPlaying;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"MediaIsPlaying Error:"<<e.what();
    }
}

void RobotWoker::slot_asr_build(QString strBNF)
{
    try{
        m_prxAudio->AsrBuild(ToUtf8(strBNF));
        qDebug()<<"AsrBuild:"<<strBNF;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"AsrBuild Error:"<<e.what();
    }
}

void RobotWoker::slot_asr_gramma(QString strName)
{
    try{
        m_prxAudio->AsrSetGramma(ToUtf8(strName));
        qDebug()<<"AsrSetGramma:"<<strName;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"AsrSetGramma Error:"<<e.what();
    }
}

void RobotWoker::slot_asr_start(int nMaxDelayMs, bool bCmdMode)
{
    try{
        m_prxAudio->AsrStart(nMaxDelayMs,bCmdMode);
        qDebug()<<"AsrStart:"<<nMaxDelayMs;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"AsrStart Error:"<<e.what();
    }
}

void RobotWoker::slot_asr_stop()
{
    try{
        m_prxAudio->AsrStop();
        qDebug()<<"AsrStop";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"AsrStop Error:"<<e.what();
    }
}

void RobotWoker::slot_head_enable(bool bHeadLR, bool bHeadUD)
{
    try{
        m_prxMotor->HeadEnable(bHeadLR,bHeadUD);
        qDebug()<<"HeadEnable";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadEnable Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_r_enable(bool bHandRExpand, bool bHandRSwing)
{
    try{
        m_prxMotor->HandREnable(bHandRExpand,bHandRSwing);
        qDebug()<<"HandREnable";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandREnable Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_l_enable(bool bHandLExpand, bool bHandLSwing)
{
    try{
        m_prxMotor->HandLEnable(bHandLExpand,bHandLSwing);
        qDebug()<<"HandLEnable";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandLEnable Error:"<<e.what();
    }
}

void RobotWoker::slot_head_lr_init()
{
    try{
        m_prxMotor->HeadLrInit();
        qDebug()<<"HandLEnable";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadLrInit Error:"<<e.what();
    }
}

void RobotWoker::slot_head_ud_init()
{
    try{
        m_prxMotor->HeadUdInit();
        qDebug()<<"HeadUdInit";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadUdInit Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_r_expand_init()
{
    try{
        m_prxMotor->HandRExpandInit();
        qDebug()<<"HandRExpandInit";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandRExpandInit Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_l_expand_init()
{
    try{
        m_prxMotor->HandLExpandInit();
        qDebug()<<"HandLExpandInit";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandLExpandInit Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_r_swing_init()
{
    try{
        m_prxMotor->HandRSwingInit();
        qDebug()<<"HandRSwingInit";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandRSwingInit Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_l_swing_init()
{
    try{
        m_prxMotor->HandLSwingInit();
        qDebug()<<"HandLSwingInit";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandLSwingInit Error:"<<e.what();
    }
}

void RobotWoker::slot_head_lr(int nHeadLRAngel, int nHeadLRSpeed)
{
    try{
        m_prxMotor->HeadLrAngle(nHeadLRAngel,nHeadLRSpeed);
        qDebug()<<"HeadLrAngle";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadLrAngle Error:"<<e.what();
    }
}

void RobotWoker::slot_head_ud(int nHeadUDAngel, int nHeadUDSpeed)
{
    try{
        m_prxMotor->HeadUdAngle(nHeadUDAngel,nHeadUDSpeed);
        qDebug()<<"HeadUdAngle";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadUdAngle Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_r_expand(int nHandRExpandAngel, int nHandRExpandSpeed)
{
    try{
        m_prxMotor->HandRExpandAngle(nHandRExpandAngel,nHandRExpandSpeed);
        qDebug()<<"HandRExpandAngle";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandRExpandAngle Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_l_expand(int nHandLExpandAngel, int nHandLExpandSpeed)
{
    try{
        m_prxMotor->HandLExpandAngle(nHandLExpandAngel,nHandLExpandSpeed);
        qDebug()<<"HandLExpandAngle";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandLExpandAngle Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_r_swing(int nHandRSwingAngel, int nHandRSwingSpeed)
{
    try{
        m_prxMotor->HandRSwingAngle(nHandRSwingAngel,nHandRSwingSpeed);
        qDebug()<<"HandRSwingAngle";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandRSwingAngle Error:"<<e.what();
    }
}

void RobotWoker::slot_hand_l_swing(int nHandLSwingAngel, int nHandLSwingSpeed)
{
    try{
        m_prxMotor->HandLSwingAngle(nHandLSwingAngel,nHandLSwingSpeed);
        qDebug()<<"HandLSwingAngle";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HandLSwingAngle Error:"<<e.what();
    }
}

void RobotWoker::slot_wheel_run(int nWheelDistance, int nWheelSpeed)
{
    try{
        m_prxMotor->WheelRun(nWheelDistance,nWheelSpeed);
        qDebug()<<"WheelRun";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"WheelRun Error:"<<e.what();
    }
}

void RobotWoker::slot_wheel_rotate(int nWheelAngel, int nWheelSpeed)
{
    try{
        m_prxMotor->WheelRotate(nWheelAngel,nWheelSpeed);
        qDebug()<<"WheelRotate";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"WheelRotate Error:"<<e.what();
    }
}

void RobotWoker::slot_wheel_run_diff(bool bLeft, int nLeftSpeed, bool bRight, int nRightSpeed)
{
    try{
        m_prxMotor->WheelRunDiff(bLeft,nLeftSpeed,bRight,nRightSpeed);
        qDebug()<<"WheelRunDiff";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"WheelRunDiff Error:"<<e.what();
    }
}

void RobotWoker::slot_wheel_stop(bool bStop)
{
    try{
        m_prxMotor->WheelStop(bStop);
        qDebug()<<"WheelStop";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"WheelStop Error:"<<e.what();
    }
}

void RobotWoker::slot_script_play_path(QString strFilePath)
{
    try{
        m_prxScript->begin_Play(ToUtf8(strFilePath));
        qDebug()<<"begin_Play";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"begin_Play Error:"<<e.what();
    }
}

void RobotWoker::slot_script_play_content(QString strContent)
{
    try{
        m_prxScript->begin_PlayContent(ToUtf8(strContent));
        qDebug()<<"play_content";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"play_content Error:"<<e.what();
    }
}

void RobotWoker::slot_script_play_stop()
{
    try{
        m_prxScript->Stop();
        qDebug()<<"Stop";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"Stop Error:"<<e.what();
    }
}

void RobotWoker::slot_script_playing()
{
    try{
        bool bPlaying = m_prxScript->begin_IsPlaying();
        qDebug()<<"script_playing";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"script_playing Error:"<<e.what();
    }
}

void RobotWoker::slot_json_load(QString strFilePath)
{
    try{
        m_prxJson->Load(ToUtf8(strFilePath));
        qDebug()<<"slot_json_load";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"slot_json_load Error:"<<e.what();
    }
}

void RobotWoker::slot_json_key_name(QString strKeyName)
{
    try{
        m_prxJson->CallByName(ToUtf8(strKeyName));
        qDebug()<<"CallByName";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"CallByName Error:"<<e.what();
    }
}

void RobotWoker::slot_json_class_name(QString strClassName)
{
    try{
        m_prxJson->CallByClass(ToUtf8(strClassName));
         qDebug()<<"CallByClass";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"CallByClass Error:"<<e.what();
    }
}

void RobotWoker::slot_json_stop()
{
    try{
        m_prxJson->Stop();
         qDebug()<<"json Stop";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"json stop Error:"<<e.what();
    }
}

void RobotWoker::slot_json_auto_play()
{
    try{
        m_prxJson->AutoPlay();
         qDebug()<<"AutoPlay";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"AutoPlay Error:"<<e.what();
    }
}

void RobotWoker::slot_json_playing()
{
    try{
        bool bPlaying = m_prxJson->IsPlaying();
         qDebug()<<"IsPlaying";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"IsPlaying Error:"<<e.what();
    }
}

void RobotWoker::slot_json_list()
{
    Ice::StringSeq strList;
    try{
         m_prxJson->List(strList);
         qDebug()<<"List";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"List Error:"<<e.what();
    }
}

void RobotWoker::slot_led_head_eye(int nIndexF, int nIndexB)
{
    try{
        m_prxMotor->HeadEye(nIndexF,nIndexB);
        qDebug()<<"HeadEye";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadEye Error:"<<e.what();
    }
}

void RobotWoker::slot_led_head_eye_2(int nDelay, bool bR, bool bG, bool bB)
{
    try{
        m_prxMotor->HeadEye2(nDelay,bR,bG,bB);
        qDebug()<<"HeadEye2";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadEye2 Error:"<<e.what();
    }
}

void RobotWoker::slot_led_head_ear(int nDelay, bool bR, bool bG, bool bB)
{
    try{
        m_prxMotor->HeadEar(nDelay,bR,bG,bB);
        qDebug()<<"HeadEar";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"HeadEar Error:"<<e.what();
    }
}

void RobotWoker::slot_led_body(int nDelay, bool bR, bool bG, bool bB)
{
    try{
        m_prxMotor->BodyLed(nDelay,bR,bG,bB);
        qDebug()<<"BodyLed";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"BodyLed Error:"<<e.what();
    }
}

void RobotWoker::slot_led_touch_set(int nMode, int nColor, int nFreq)
{
    try{
        m_prxMotor->TouchSet(nMode,nColor,nFreq);
        qDebug()<<"TouchSet";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"TouchSet Error:"<<e.what();
    }
}

void RobotWoker::slot_set_avoid(bool bAvoid)
{
    try{
        m_prxMotor->SetProtect(bAvoid);
        qDebug()<<"SetProtect";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"SetProtect Error:"<<e.what();
    }
}

void RobotWoker::slot_get_avoid()
{
    try{
        bool bAvoid = m_prxMotor->GetProtect();
        qDebug()<<"GetProtect:"<<bAvoid;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"GetProtect Error:"<<e.what();
    }
}

void RobotWoker::slot_set_system_volume(int nVolume)
{
    try{
        m_prxConfig->SetSystemVolume(nVolume);
        qDebug()<<"SetSystemVolume";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"SetSystemVolume Error:"<<e.what();
    }
}

void RobotWoker::slot_get_system_volume()
{
    try{
        int nVolume = m_prxConfig->GetSystemVolume();
        qDebug()<<"GetSystemVolume:"<<nVolume;
    }catch(Ice::Exception& e)
    {
        qDebug()<<"GetSystemVolume Error:"<<e.what();
    }
}

void RobotWoker::slot_set_system_cmd(QString strCmd)
{
    try{
        m_prxConfig->SetSystemCmd(ToUtf8(strCmd));
        qDebug()<<"SetSystemCmd";
    }catch(Ice::Exception& e)
    {
        qDebug()<<"SetSystemCmd Error:"<<e.what();
    }
}

void RobotWoker::slot_notify(int state, const QString &content)
{
    switch (state) {
    case MySessionCB::STT_CREATEDCOMMUNICATOR:

        break;
    case MySessionCB::STT_CONNECTED:
        getPrx();
        break;
    case MySessionCB::STT_DISCONNECTED:
        _session = 0;
        _factory->destroy();
        break;
    case MySessionCB::STT_CONNECTFAILED:
        break;
    default:
        break;
    }
    qDebug()<<content;
}
